from sqlalchemy import Column, Integer, Date, String, ForeignKey, Boolean
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import relationship


Base = declarative_base()


class User(Base):
    __tablename__ = "users"

    id = Column(Integer, primary_key=True, index=True)
    username = Column(String, unique=True, index=True)
    login = Column(String, unique=True, index=True)
    hashed_password = Column(String)
    is_active = Column(Boolean, default=1)
    role_id = Column(Integer, ForeignKey("roles.id"))
    modality_id = Column(Integer, ForeignKey("modalities.id"), nullable=True)
    employment_rate = Column(String, nullable=True)
    start_work = Column(Date, nullable=True)
    status = Column(String, nullable=True)

    role = relationship("Role", back_populates="users")
    modality = relationship("Modality", back_populates="users")
    additional_modalities = relationship(
        "Modality", secondary="user_modality", back_populates="additional_users"
    )
    absent = relationship('DoctorAbsent', back_populates='users')


class Modality(Base):
    __tablename__ = "modalities"

    id = Column(Integer, primary_key=True, index=True)
    name = Column(String, unique=True)

    users = relationship("User", back_populates="modality")
    additional_users = relationship(
        "User", secondary="user_modality", back_populates="additional_modalities"
    )


class UserModality(Base):
    __tablename__ = "user_modality"

    user_id = Column(Integer, ForeignKey("users.id"), primary_key=True)
    modality_id = Column(Integer, ForeignKey("modalities.id"), primary_key=True)


class Role(Base):
    __tablename__ = "roles"

    id = Column(Integer, primary_key=True, index=True)
    name = Column(String, unique=True)
    fullname = Column(String)

    users = relationship("User", back_populates="role")


class FileStatistic(Base):
    __tablename__ = "file_statistics"

    id = Column(Integer, primary_key=True, index=True)
    filename = Column(String, nullable=False, unique=True)


class DoctorAbsent(Base):
    __tablename__ = 'doctor_absent'

    id = Column(Integer, primary_key=True, index=True)
    id_doctor = Column(ForeignKey('users.id'))
    reason = Column(String, doc='Причина отсутствия')
    start_absent = Column(Date, doc='Дата начала отсутствия')
    quantity_days = Column(Integer, doc='Количестов дней отсутствия')
    viewed = Column(Boolean, doc='Просмотрено')

    users = relationship('User', back_populates='absent')


class Statistic(Base):
    __tablename__ = "statistics"

    id = Column(Integer, primary_key=True, index=True)
    id_filename = Column(Integer, ForeignKey("file_statistics.id"))
    year = Column(Integer, nullable=False)
    week = Column(Integer, nullable=False)
    date = Column(Date)
    wc = Column(Integer, nullable=False)
    kind = Column(String, nullable=False)
    modality = Column(String, nullable=False)
    type = Column(String, nullable=False)
    number = Column(Integer, nullable=False)


class FileForecast(Base):
    __tablename__ = "file_forecasts"

    id = Column(Integer, primary_key=True, index=True)
    filename = Column(String, nullable=False, unique=True)


class Forecast(Base):
    __tablename__ = "forecasts"

    id = Column(Integer, primary_key=True, index=True)
    id_filename = Column(Integer, ForeignKey("file_forecasts.id"))
    date = Column(Date, nullable=False)
    week = Column(Integer, nullable=False)
    wc = Column(Integer, nullable=False)
    modality = Column(String, nullable=False)
    type = Column(String, nullable=False)
    kind = Column(String, nullable=False)
    number = Column(Integer, nullable=False)
    workhours = Column(Integer, nullable=False)


class Schedule(Base):
    __tablename__ = "schedules"

    id = Column(Integer, primary_key=True, index=True)
    schedule_name = Column(String, nullable=False, doc="Название расписания")
    date = Column(Date, nullable=False)
    name = Column(String, nullable=False, doc="Имя врача")
    modality = Column(String, nullable=False, doc="Модальность")
    week = Column(Integer, nullable=False, doc="Номер недели")
    workhours = Column(Integer, nullable=False, doc="Количество рабочих часов")
    ready = Column(Boolean, nullable=False, default=False, doc="Готовность расписания")

    def to_dict(self):
        return {
            'id': self.id,
            'schedule_name': self.schedule_name,
            'date': self.date,
            'name': self.name,
            'modality': self.modality,
            'week': self.week,
            'weekday': self.weekday,
            'workhours': self.workhours,
        }


class ScheduleName(Base):
    __tablename__ = "schedule_names"

    id = Column(Integer, primary_key=True, index=True)
    name = Column(String, nullable=False, unique=True, doc="Название расписания")


class ApprovedSchedule(Base):
    __tablename__ = "approved_schedule"

    id = Column(Integer, primary_key=True, index=True)
    name = Column(String, nullable=False, unique=True, doc="Название расписания")
